package http;

import java.util.HashMap;
import java.util.Map;

import com.google.gson.Gson;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

/**
 * 未测试 建议使用HTTP客户端（Hutool-http）
 *
 * 参考链接:https://blog.csdn.net/justry_deng/article/details/81042379
 */
public class HttpPostUtil {

    public static String doPost(HttpClient httpClient, String url, Map<String, String> paramHeader, String paramBody, String charset) throws Exception {
        String result = null;
        HttpPost httpPost = new HttpPost(url);

        setHeader(httpPost, paramHeader);
        setBody(httpPost, paramBody, charset);
//        setConfig(httpPost);

        HttpResponse response = httpClient.execute(httpPost);
        if (response != null) {
            HttpEntity responseEntity = response.getEntity();
            if (responseEntity != null) {
                result = EntityUtils.toString(responseEntity, charset);
            }
        }

        return result;
    }

    private static void setHeader(HttpRequestBase httpPost, Map<String, String> paramHeader) {
        if (paramHeader != null) {
            paramHeader.forEach((key, value) -> {
                httpPost.addHeader(key, value);
            });
        }
    }

    private static void setConfig(HttpRequestBase httpPost) {
        // 配置信息
        RequestConfig requestConfig = RequestConfig.custom()
                // 设置连接超时时间(单位毫秒)
                .setConnectTimeout(5000)
                // 设置请求超时时间(单位毫秒)
                .setConnectionRequestTimeout(5000)
                // socket读写超时时间(单位毫秒)
                .setSocketTimeout(5000)
                // 设置是否允许重定向(默认为true)
                .setRedirectsEnabled(true).build();

        // 将上面的配置信息 运用到这个Get请求里
        httpPost.setConfig(requestConfig);
    }

    private static void setBody(HttpPost httpPost, String paramBody, String charset) throws Exception {
        if (paramBody.length() > 0) {
            StringEntity s = new StringEntity(paramBody, "UTF-8");
            s.setContentEncoding("UTF-8");
            s.setContentType("application/json");
            httpPost.setEntity(s);

            System.out.println(paramBody);
            System.out.println(s);
        }

    }

    public static void main(String[] args) throws Exception {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        String url = "https://5141176-sb1.restlets.api.netsuite.com/app/site/hosting/restlet.nl?script=391&deploy=1";

        Map<String, String> paramHeader = new HashMap();
        paramHeader.put("Content-Type", "application/json;charset=utf8");

        Gson gson = new Gson();
        Map<String, String> paramBody = new HashMap();
        paramBody.put("name", "wwj");
        paramBody.put("password", "123");
        String paramBodyJson = gson.toJson(paramBody);

        String result = doPost(httpClient, url, paramHeader, paramBodyJson, "UTF-8");
        System.out.println(result);
    }
}
